<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2021-09-06
 * Time: 16:59
 */

namespace app\admin\controller;


use app\admin\BaseController;

/**
 * Class BroadcastController
 * @package app\admin\controller
 */
class BroadcastController extends BaseController
{
    /**
     * @return \think\response\Json
     */
    public function listsAction()
    {
        $get = $this->request->get();

        try {
            $params = [
                'where' => $this->_get_cond($get),
                'order' => [
                    'sort' => 'desc'
                ]
            ];
            $broadcasts = $this->app->broadcastService->getLists($params, $this->page, $this->pagesize);

            $data['lists'] = [];
            if (count($broadcasts)) {
                foreach ($broadcasts as $broadcast) {
                    $data['lists'][] = [
                        'broadcastId' => $broadcast->broadcastId,
                        'broadcastName' => $broadcast->broadcastName,
                        'broadcastUrl' => $broadcast->broadcastUrl,
                        'broadcastImage' => $broadcast->broadcastImage,
                        'sort' => $broadcast->sort,
                        'status' => $broadcast->status,
                        'statusText' => $broadcast->getStatusText(),
                        'createdAt' => $broadcast->createdAt
                    ];
                }
            }
            $data['totalInfo'] = $this->app->utilService->getMeta($broadcasts);

            return json(['code' => 200, 'msg' => '查询成功', 'data' => $data]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function detailAction($id)
    {
        try {
            $broadcast = $this->app->broadcastService->getDetail($id);

            $data = $broadcast->toArray();

            return json(['code' => 200, 'msg' => '查询成功', 'data' => $data]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @return \think\response\Json
     */
    public function addAction()
    {
        try {
            $params = $this->post;
            $params['appletId'] = $this->appletId?:1;
            $this->app->broadcastService->create($params);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function updateAction($id)
    {
        try {
            $cooperation = $this->app->broadcastService->getDetail($id);

            $this->app->broadcastService->update($cooperation, $this->post);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function deleteAction($id)
    {
        try {
            $cooperation = $this->app->broadcastService->getDetail($id);

            $this->app->broadcastService->delete($cooperation);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function enableAction($id)
    {
        try {
            $cooperation = $this->app->broadcastService->getDetail($id);

            $this->app->broadcastService->enable($cooperation);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function disableAction($id)
    {
        try {
            $cooperation = $this->app->broadcastService->getDetail($id);

            $this->app->broadcastService->disable($cooperation);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * 获取轮播图位置
     * @return \think\response\Json
     */
    public function getPositionAction()
    {
        try {
            $positions = $this->app->broadcastService->getPositions();

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $positions]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $params
     * @return array
     */
    private function _get_cond($params)
    {
        $cond = [];

        $cond[] = ['appletId', '=', $this->appletId];

        if (isset($params['cooperationName']) && $params['cooperationName'] != '') {
            $cond[] = ['cooperationName', 'like', '%'.$params['cooperationName'].'%'];
        }

        return $cond;
    }
}